//
//  ViewController.m
//  pres
//
//  Created by xingshuai on 2021/1/8.
//

#import "ViewController.h"

#import "TTTViewController.h"

@interface ViewController ()
{
    UIButton *button;
    UIButton *btn;
}
@property (nonatomic, strong) NSMutableArray *imgList;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = UIColor.cyanColor;
    
    button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    button.backgroundColor = UIColor.orangeColor;
    button.frame = CGRectMake(100, 100, 150, 150);
    [button addTarget:self action:@selector(clickBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(400, 500, 60, 60);
    btn.backgroundColor = UIColor.greenColor;
    [btn setImage:[UIImage imageNamed:@"favour_0000"] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(didBtn:) forControlEvents:UIControlEventTouchUpInside];
    btn.imageView.animationImages = self.imgList;
    btn.imageView.animationDuration = 0.54;
    btn.imageView.animationRepeatCount = 1;
    [self.view addSubview:btn];
    
}

-(void)clickBtn {
    TTTViewController *vc = [[TTTViewController alloc]init];
    vc.transitionAnimation.curRect = button.frame;
    vc.modalPresentationStyle = UIModalPresentationCustom;
    [self presentViewController:vc animated:YES completion:nil];
}

-(NSMutableArray *)imgList {
    if (!_imgList) {
        _imgList = [[NSMutableArray alloc]init];
        for (int i = 0; i <= 20; i++) {
            UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"favour_000%d", i]];
            [_imgList addObject:image];
        }
    }
    return _imgList;
}

-(void)didBtn:(UIButton *)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [sender.imageView startAnimating];
        [self performSelector:@selector(animationDidFinish) withObject:self afterDelay:0.54];
    } else {
        [sender.imageView stopAnimating];
    }
    
}

// 点赞动画执行结束
- (void)animationDidFinish {
    [btn.imageView stopAnimating];
    [btn setImage:[UIImage imageNamed:@"favour_00020"] forState:UIControlStateSelected];
}

@end
